/**
 * 
 */
package idv.takeshi.software.productline.bookstore.domain.model.useraccount;

import java.util.UUID;

import org.apache.commons.lang.Validate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A model factory for generating the user account model. 
 * @author takeshi
 *
 */
public class UserAccountFactory {
	
	private static final Log log = LogFactory.getLog(UserAccountFactory.class);
	
	
	/**
	 * Generate a user account instance.
	 * @param <T> A A sub-classes of UserAccount.
	 * @param clazz A sub-classes of UserAccount.
	 * @param accountName A account name.
	 * @return
	 */
	public <T extends UserAccount>T newInstance(Class<T> clazz, String accountName) {
		T userAccount = null;
		try {
			Validate.notNull(clazz, "given UserAccount class is null");
			Validate.notEmpty(accountName, "given userName is null/empty");
			userAccount = clazz.newInstance();
		} catch (Exception e) {
			String msg = "create user account failed";
			log.error(msg, e);
			throw new CreateUserAccountFailedException(msg, e);
		}
        userAccount.setAccountName(accountName.toLowerCase());
        userAccount.setEnabled(false);
        userAccount.setNonExpired(true);
        userAccount.setNonLocked(true);
        userAccount.setCredentialsNonExpired(true);
        userAccount.setActivationKey(UUID.randomUUID().toString());
		
		return userAccount;
	}
}
